package com.gem.day17;

import java.io.InputStream;
import java.util.Properties;

/**
 * 利用反射机制动态创建类对象的工具类,
 * 类似于后期spring框架的IOC容器
 */
public class BeanFactory {

	static Properties prop;

	static {
		try {
			prop = new Properties();
			InputStream inStream = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("bean.properties");
			prop.load(inStream);
		} catch (Exception e) {
			System.err.println("配置文件加载有误...");
		}
	}

	public static Object getBean(String className) {
		Object obj = null;
		try {
			Class<?> c = Class.forName(prop.getProperty(className));
			obj = c.getConstructor().newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
}
